﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;

namespace SyncServer
{
    //公共类
    public class Common : Page
    {

    


        #region "压缩数据"
        /// <summary>
        /// 压缩数据
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static byte[] Compress(String str)
        {
            try
            {
                byte[] sourse = System.Text.Encoding.UTF8.GetBytes(str);
                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.GZip.GZipOutputStream stream = new GZipOutputStream(ms);
                stream.Write(sourse, 0, sourse.Length);
                stream.Close();
                byte[] data = ms.ToArray();
                
                return data;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

        #region "解压缩"
        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static String Decompress(Stream inputStream)
        {
            string str = string.Empty;
            GZipInputStream stream = new GZipInputStream(inputStream);
            MemoryStream output = new MemoryStream();
            try
            {
                
                byte[] buffer = new byte[4096];
                int read = -1;
                read = stream.Read(buffer, 0, buffer.Length);
                while (read > 0)
                {
                    output.Write(buffer, 0, read);
                    read = stream.Read(buffer, 0, buffer.Length);
                }
                str = System.Text.Encoding.UTF8.GetString(output.ToArray(), 0, (int)output.Length);
                
            }
            catch
            {
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }
                if (output != null)
                {
                    output.Close();
                    output.Dispose();
                    output = null;
                }
            }
            return str;
        }
        #endregion

        #region "解压缩"
        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static String ToString(Stream inputStream)
        {
            string str = string.Empty;
            MemoryStream output = new MemoryStream();
            try
            {

                byte[] buffer = new byte[4096];
                int read = -1;
                read = inputStream.Read(buffer, 0, buffer.Length);
                while (read > 0)
                {
                    output.Write(buffer, 0, read);
                    read = inputStream.Read(buffer, 0, buffer.Length);
                }
                str = System.Text.Encoding.UTF8.GetString(output.ToArray(), 0, (int)output.Length);
              
            }
            catch
            {
                throw;
            }
            finally
            {
                if (output != null)
                {
                    output.Close();
                    output.Dispose();
                    output = null;
                }
                if (inputStream != null)
                {
                    inputStream.Close();
                    inputStream.Dispose();
                    inputStream = null;
                }
            }
            return str;
        }
        #endregion

        public static string getNewImageName()
        {
            Random r = new Random();
            return DateTime.Today.ToString("yyyyMMddHHmmss") + r.Next(1, 1000) + r.Next(1, 1000);
        }

    }
}
